<?php
namespace app\common\model;

use think\Model;

class FreightModel extends Model {
    // 完整数据表名
    protected $table              = 'freight';
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    // 时间输出格式
    protected $type = [
        'freight_create_time' => 'timestamp',
        'freight_update_time' => 'timestamp',
    ];

    //商户自提点列表
    public static function freight_sel($where, $page_number){
        $freight = FreightModel::where($where)->order('freight_create_time desc')->paginate($page_number);
        if(!$freight){
            return false;
        }
        return $freight;
    }

    //添加自提点数据
    public static function freight_add($data){
        $freight = FreightModel::create($data);
        if(!$freight){
            return false;
        }
        return $freight;
    }

    //修改某个商户自提点
    public static function freight_edit($data, $where){
        $freight = FreightModel::update($data, $where);
        if(!$freight){
            return false;
        }
        return $freight;
    }

    //删除商户自提点
    public static function freight_del($where){
        $freight = FreightModel::destroy($where);
        if(!$freight){
            return false;
        }
        return true;
    }


    
    // 运营后台快递模板列表
    /*
    *@time:2017-7-21
    *@auther:syf
    */
    public static function rapidExpressTpl($pagesize)
    {
        $where['freignt_is_delete'] = ['eq', 0];
        $where['freight_tpl_type'] = ['eq', 1];

        // 模糊搜索
        $keyword = input('param.keyword');
        if($keyword)
            $where['a.freight_name|c.store_name'] = ['like', "%$keyword%"];

        $data = FreightModel::alias('a')
                ->field('a.freight_tpl_type,a.freight_id,a.freight_name,a.freight_distribution_province_address,a.freight_first_price,a.freight_continued_price,c.store_id,c.store_name')
                ->join('merchant b', 'a.freight_merchant_id=b.merchant_id', 'left')
                ->join('store c', 'b.merchant_id=c.store_merchant_id', 'left')
                ->where($where)
                ->order('freight_id', 'desc')
                ->paginate($pagesize);
        return $data;
    }

    // 查看快递模板
    /*
    *@time:2017-7-21
    *@auther:syf
    */
    public static function getExpressTpl($freight_id, $tplType)
    {
        $where['freight_id'] = ['eq', $freight_id];
        $where['freignt_is_delete'] = ['eq', 0];
        $where['freight_tpl_type'] = ['eq', $tplType];
        $data = FreightModel::where($where)->find();
        return $data;
    }

    /*
    *@time:2017-7-21
    *@auther:syf
    */
    public static function toDoorTpl($pagesize)
    {
        $where['freignt_is_delete'] = ['eq', 0];
        $where['freight_tpl_type'] = ['eq', 2];

        // 模糊搜索
        $keyword = input('param.keyword');
        if($keyword)
            $where['a.freight_name|c.store_name'] = ['like', "%$keyword%"];

        $data = FreightModel::alias('a')
                ->field('a.freight_tpl_type,a.freight_id,a.freight_name,a.freight_distribution_province_address,a.freight_door_cost,c.store_id,c.store_name')
                ->join('merchant b', 'a.freight_merchant_id=b.merchant_id', 'left')
                ->join('store c', 'b.merchant_id=c.store_merchant_id', 'left')
                ->where($where)
                ->order('freight_id', 'desc')
                ->paginate($pagesize);
        
        return $data;
    }

    /*
    *@time:2017-7-21
    *@auther:syf
    */
    public static function changeDel($id)
    {
        $result = FreightModel::where('freight_id', $id)->setField('freignt_is_delete', 1);

        if(!$result)
            return false;

        return true;
    }  

}